package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.Customer;
import com.easondongh.business.domain.Goods;
import com.easondongh.business.domain.Provider;
import com.easondongh.business.mapper.CustomerMapper;
import com.easondongh.business.mapper.GoodsMapper;
import com.easondongh.business.mapper.ProviderMapper;
import com.easondongh.business.vo.CustomerQueryVo;
import com.easondongh.business.vo.GoodsImportQueryVo;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.mapper.GoodsImportMapper;
import com.easondongh.business.domain.GoodsImport;
import com.easondongh.business.service.GoodsImportService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author EasonDongH
 * @date 2020/6/1 14:11
 */
@Service
public class GoodsImportServiceImpl extends ServiceImpl<GoodsImportMapper, GoodsImport> implements GoodsImportService {

    @Autowired
    private GoodsImportMapper goodsImportMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public PageResult<GoodsImport> getByPage(BasePageQueryVo<GoodsImportQueryVo> queryCondition) {
        IPage<GoodsImport> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<GoodsImport> queryWrapper = new QueryWrapper<>();
        GoodsImportQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.eq(queryParams.getProviderId() != null, GoodsImport.COL_PROVIDER_ID, queryParams.getProviderId());
            queryWrapper.eq(queryParams.getGoodsId() != null, GoodsImport.COL_GOODS_ID, queryParams.getGoodsId());
            queryWrapper.gt(queryParams.getStartTime() != null, GoodsImport.COL_IMPORT_TIME, queryParams.getStartTime());
            queryWrapper.lt(queryParams.getEndTime() != null, GoodsImport.COL_IMPORT_TIME, queryParams.getEndTime());
        }
        queryWrapper.orderByDesc(GoodsImport.COL_IMPORT_TIME);
        IPage<GoodsImport> iPage = this.goodsImportMapper.selectPage(page, queryWrapper);
        List<GoodsImport> goodsImportList = iPage.getRecords();
        if(!CollectionUtils.isEmpty(goodsImportList)){
            List<Provider> providerList = this.providerMapper.getAllEnableProvider();
            List<Goods> goodsList = this.goodsMapper.getAllEnableGoods();
            Map<Integer, String> providerMap = new HashMap<>();
            Map<Integer, String> goodsMap = new HashMap<>();
            if(!CollectionUtils.isEmpty(providerList)) {
                providerMap = providerList.stream().collect(Collectors.toMap(Provider::getId, Provider::getProviderName));
            }
            if(!CollectionUtils.isEmpty(goodsList)) {
                goodsMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getGoodsName));
            }
            for (GoodsImport goodsImport : goodsImportList) {
                goodsImport.setGoodsName(goodsMap.get(goodsImport.getGoodsId()));
                goodsImport.setProviderName(providerMap.get(goodsImport.getProviderId()));
            }
        }
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), goodsImportList);
    }

    @Transactional
    @Override
    public void addGoodsImport(GoodsImport goodsImport) {
        this.goodsImportMapper.insert(goodsImport);
        this.goodsMapper.updateStock(goodsImport.getGoodsId(), goodsImport.getNumber());
    }

    @Transactional
    @Override
    public void updateGoodsImport(GoodsImport goodsImport) {
        GoodsImport dbData = this.goodsImportMapper.selectById(goodsImport.getId());
        int diff = goodsImport.getNumber() - dbData.getNumber();
        this.goodsMapper.updateStock(goodsImport.getGoodsId(), diff);
        this.goodsImportMapper.updateById(goodsImport);
    }

    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        List<GoodsImport> importList = this.goodsImportMapper.selectBatchIds(ids);
        for (GoodsImport goodsImport : importList) {
            this.goodsMapper.updateStock(goodsImport.getGoodsId(), 0 - goodsImport.getNumber());
        }
        this.goodsMapper.deleteBatchIds(ids);
    }
}
